package com.yc.home.cloudother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Result;
import com.yc.entity.Doquestion;
import com.yc.entity.Question;
import com.yc.home.cloudother.mapper.DoquestionMapper;
import com.yc.home.cloudother.mapper.QuestionMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzk
 * @since 2024-09-23
 */
@RestController
@RequestMapping("/doquestion")
public class DoquestionController {

    @Autowired
    DoquestionMapper doquestionMapper;

    @Autowired
    QuestionMapper questionMapper;

    @RequestMapping("getqusetion")
    Page getqusetion(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "10") int size, String input, String uid, String start, String end, int selecttf){
        Page<Doquestion> pageObj = new Page<>(page, size);

        // 条件设置
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getUid, uid);
        if(!"".equals(start) && !"".equals(end)){
            lqw.ge(Doquestion::getCreatetime, start);
            lqw.le(Doquestion::getCreatetime, end);
        }
        if(selecttf != -1){
            lqw.eq(Doquestion::getTf, selecttf);
        }

        // 执行分页查询
        Page<Doquestion> makenoPage = doquestionMapper.selectPage(pageObj, lqw);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 使用Stream API转换Doquestion为Question并更新Page对象的records
        List<doqusetion> questionList = makenoPage.getRecords().stream()
                .map(doquestion -> {
                    LambdaQueryWrapper<Question> lqw1 = new LambdaQueryWrapper<>();
                    lqw1.eq(Question::getId, doquestion.getQid());
                    lqw1.like(Question::getContent, "%"+ input +"%");
                    Question question = questionMapper.selectOne(lqw1);
                    if(question == null){
                        return null;
                    }
                    doqusetion doq = new doqusetion();
                    doq.setAnswer(doquestion.getAnswer());
                    doq.setId(question.getId());
                    doq.setContent(question.getContent());
                    doq.setCourse(question.getCourse());
                    doq.setIdeas(question.getIdeas());
                    doq.setItems(question.getItems());
                    doq.setImage(question.getImage());
                    doq.setCreateTime(question.getCreateTime());
                    doq.setType(question.getType());
                    doq.setDid(String.valueOf(doquestion.getId()));
                    doq.setDocreate(sdf.format(doquestion.getCreatetime()));
                    doq.setSum(doquestion.getSum());
                    doq.setTf(doquestion.getTf());
                    return doq;
                }).filter(Objects::nonNull)
                .collect(Collectors.toList());

        //替换数据
        Page<doqusetion> questionPage = new Page<>(makenoPage.getCurrent(), makenoPage.getSize(), makenoPage.getTotal());
        questionPage.setRecords(questionList);
        return questionPage;
    }

    //获取当天做题数量
    @RequestMapping("getday")
    Result getday(String uid){
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 将当前日期转换为 LocalDateTime，时间设置为00:00:00
        LocalDateTime startOfDay = today.atStartOfDay();
        // 将当前日期转换为 LocalDateTime，时间设置为23:59:59
        LocalDateTime endOfDay = today.atTime(LocalTime.MAX);
        LambdaQueryWrapper<Doquestion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doquestion::getUid, uid);
        lqw.between(Doquestion::getCreatetime, startOfDay, endOfDay);
        Long aLong = doquestionMapper.selectCount(lqw);
        return new Result(1, "今日做题数量", aLong);
    }

    //删除刷题信息
    @RequestMapping("del")
    Result del(String id){
        try{
            doquestionMapper.deleteById(id);
            return new Result(1, "删除成功", null);
        }catch (Exception e){
            return new Result(0, "删除失败", null);
        }
    }

    //获取一周的刷题量
    @RequestMapping("getweekordernum")
    Result getweekordernum(String id){
        List<Map<String, String>> getweeknum = doquestionMapper.getweeknum(id);
        return new Result(1, "近7日刷题数量", getweeknum);
    }

    //获取前五的数据
    @RequestMapping("getFiveQuestionall")
    Result getFiveQuestionall(){
        List<Map<String, String>> getfive = doquestionMapper.getallfive();
        return new Result(1, "前5数据", getfive);
    }

    //获取前五的每日数据
    @RequestMapping("getFiveQuestion")
    Result getFiveQuestion(){
        List<Map<String, String>> getfive = doquestionMapper.getfive();
        //当今日没有数据时显示历史前五
        if(getfive.size() == 0){
            List<Map<String, String>> getallfive = doquestionMapper.getallfive();
            return new Result(1, "前5数据", getallfive);
        }
        return new Result(1, "前5数据", getfive);
    }

    //获取刷题数量
    @RequestMapping("getquestionnum")
    Result getquestionnum(){
        // 获取当天的日期范围
        LocalDate today = LocalDate.now();
        ZonedDateTime startOfDay = today.atStartOfDay(ZoneId.systemDefault());
        ZonedDateTime endOfDay = today.plusDays(1).atStartOfDay(ZoneId.systemDefault()).minusSeconds(1);
        // 构建查询条件
        LambdaQueryWrapper<Doquestion> lqw = Wrappers.lambdaQuery();
        lqw.between(Doquestion::getCreatetime, startOfDay, endOfDay); // 假设您的实体类中有 getCreatedAt 方法

        // 执行查询并获取计数
        Long questionCount = doquestionMapper.selectCount(lqw);
        return new Result(1, "今日数量", questionCount);
    }

    @RequestMapping("getweekquestionnum")
    Result getweekquestionnum(){
        List<Map<String, String>> getweeknum = doquestionMapper.getallweeknum();
        return new Result(1, "近7日刷题数量", getweeknum);
    }
}
@Data
class doqusetion {
    private Integer id;

    private String content;

    private String course;

    private String type;

    private String items;

    private Timestamp createTime;

    private String ideas;

    private String image;

    private String answer;

    private String did;

    private String docreate;

    private Integer sum;

    private Integer tf;
}
